<?php

class ExperienceController extends Controller
{
	public function actionIndex()
	{
		$uid= Yii::app()->request->getParam('uid');
		$User= User::model()->findByPk($uid);
		$OutsideProject = new OutsideProject();
		$OutsideProjectList = OutsideProject::model()->findAllByAttributes(array('user_id'=>$uid),
														array('order'=>'outside_project_id DESC'));
		$this->performAjaxValidation($OutsideProject);
		$UserStatus= new UserStatus();
		$UserStatus = UserStatus::model()->findByPk($uid);
		$AllProgrammingLanguage = ProgrammingLanguage::model()->findAll(array('order'=>'programming_language_id'));
		$ProgList= CHtml::listData($AllProgrammingLanguage,'programming_language_id' ,'programming_language_name');
	
		
		$this->render('index',array(
							'User'=>$User,
							'OutsideProjectList'=>$OutsideProjectList,
							'UserStatus'=>$UserStatus,
							'OutsideProject'=>$OutsideProject,
							'ProgList'=>$ProgList,
		                    'uid'=>$uid,   
					));
		
	}
	public function actionAdd(){
		$uid= Yii::app()->request->getParam('uid');
		$OutsideProject = new OutsideProject();
		
		if(isset($_POST['OutsideProject'])){
		  $OutsideProject->attributes= $_POST['OutsideProject'];
		  $OutsideProject->user_id= $uid;
		  Yii::log('vao cho luu thanh cong');
		  if(OutsideProject::model()->findAllByAttributes(array('user_id'=>$uid,'outside_project_name'=>$OutsideProject->outside_project_name)))
		  {
		  Yii::app()->user->setFlash('error','Bạn đã lưu project này !');
		  }
		  else if($OutsideProject->save()){
		  	Yii::log('luu thanh cong');
		  	Yii::app()->user->setFlash('sucess','Lưu thành công');
		  
		  }
		
		}
		$OutsideProjectList = OutsideProject::model()->findAllByAttributes(array('user_id'=>$uid),
														array('order'=>'outside_project_id DESC'));
		$this->renderPartial('_outsideProjectList',array('OutsideProjectList'=>$OutsideProjectList,
											   'uid'=>$uid),false,true);
	}
	/**
	 * 
	 * Action delete an outside project...
	 * @param unknown_type $id
	 * @throws CHttpException
	 */
	
	public function actionDelete()
	{
	      // GET app parameter
	            $uid= Yii::app()->request->getParam('uid');
                $pid = Yii::app()->request->getPost("pid");
                if ($pid == null)
                	throw new CHttpException(404,'Param not enough');
                	
                $project = OutsideProject::model()->findByPk($pid);
                if (!$project || !$project->delete()){
                	throw new CHttpException(404,"Can't delete");
                }
                $OutsideProjectList = OutsideProject::model()->findAllByAttributes(array('user_id'=>$uid),
														array('order'=>'outside_project_id DESC'));
				$this->renderPartial('_outsideProjectList',array('OutsideProjectList'=>$OutsideProjectList,
											   'uid'=>$uid),false,true);
	}
	
	/**
	 * perform ajax validation
	 */
	protected function performAjaxValidation($model) {
		if (isset ( $_POST ['ajax'] ) && $_POST ['ajax'] === 'add-exp') {
			echo CActiveForm::validate ( $model );
			Yii::app ()->end ();
		}
	}
		/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
public function loadModel($id)
	{
		$model=OutsideProject::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The project does not exist.');
		return $model;
	}
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertUseyValue',
			),
		);
	}
	*/
}